<template>
  <div>
    <Category></Category>
    <el-card style="margin: 10px 0">
      <div v-show="scene == 0">
        <el-button
          type="primary"
          icon="Plus"
          @click="addSpu"
          :disabled="!categoryStore.thirdCategoryId"
        >
          添加SPU
        </el-button>
        <el-table border style="margin: 10px 0" :data="recodeds">
          <el-table-column
            type="index"
            label="序号"
            align="center"
            width="80px"
          ></el-table-column>
          <el-table-column label="SPU名称" prop="spuName"></el-table-column>
          <el-table-column
            label="SPU描述"
            prop="description"
            how-overflow-tooltip
          ></el-table-column>
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button
                type="primary"
                size="small"
                icon="Plus"
                title="添加SKU"
                @click="addSku(row)"
              ></el-button>
              <el-button
                type="warning"
                size="small"
                icon="Edit"
                title="修改SPU"
                @click="updateSpu(row)"
              ></el-button>
              <el-button
                type="info"
                size="small"
                icon="View"
                title="查看SKU列表"
                @click="lookSkuList(row)"
              ></el-button>
              <el-popconfirm
                :title="`你确定删除${row.spuName}?`"
                width="200px"
                @confirm="deleteSpu(row.id)"
              >
                <template #reference>
                  <el-button
                    type="danger"
                    size="small"
                    icon="Delete"
                    title="删除SPU"
                  ></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

        <el-pagination
          @current-change="getSpuList"
          @size-change="changeSize"
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :background="true"
          layout="sizes, prev, pager, next, ->, jumper, total"
          :total="total"
        />
      </div>
      <SpuForm v-if="scene == 1" @reback="reback" ref="spu"></SpuForm>
      <SkuForm v-if="scene == 2" @reback="reback" ref="sku"></SkuForm>
    </el-card>

    <el-dialog title="SKU列表" v-model="showSkuList">
      <el-table :data="skuList" border>
        <el-table-column label="SKU名字" prop="skuName"></el-table-column>
        <el-table-column label="SKU价格" prop="price"></el-table-column>
        <el-table-column label="SKU重量" prop="weight"></el-table-column>
        <el-table-column label="SKU图片">
          <template #default="{ row }">
            <img :src="row.skuDefaultImg" style="width: 100px; height: 100px" />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import Category from '@/components/Category/index.vue'
import { nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import useCategoryStore from '@/store/modules/category.ts'
import { reqHasSpu, reqRemoveSpu, reqSkuList } from '@/api/product/spu'
import type { SpuData } from '@/api/product/spu/type.ts'
import SkuForm from './skuForm.vue'
import SpuForm from './spuForm.vue'
import { ElMessage } from 'element-plus'
let categoryStore = useCategoryStore()
let currentPage = ref(1)
let pageSize = ref(10)
let total = ref(0)
let recodeds = ref<SpuData[]>([])
let scene = ref(0)
let spu = ref()
let sku = ref()

onMounted(() => {
  if (!categoryStore.thirdCategoryId) return
  getSpuList()
})

watch(
  () => categoryStore.thirdCategoryId,
  () => {
    if (!categoryStore.thirdCategoryId) return
    getSpuList()
  },
)

async function getSpuList(page = 1) {
  currentPage.value = page
  let result = await reqHasSpu(
    currentPage.value,
    pageSize.value,
    categoryStore.thirdCategoryId,
  )
  if (result.code === 200) {
    recodeds.value = result.data.records
    total.value = result.data.total
  }
}
//分页器下拉菜单发生变化的时候触发
const changeSize = () => {
  getSpuList()
}

function addSpu() {
  scene.value = 1
  nextTick(() => {
    spu.value.initAddData(categoryStore.thirdCategoryId)
  })
}

function updateSpu(row: any) {
  scene.value = 1
  nextTick(() => {
    spu.value.initHasSpuData(JSON.parse(JSON.stringify(row)))
  })
}

function addSku(row: any) {
  scene.value = 2
  nextTick(() => {
    const param = {
      c1Id: categoryStore.mainCategoryId,
      c2Id: categoryStore.secCategoryId,
      c3Id: categoryStore.thirdCategoryId,
      spuId: row.id,
      spu: row,
    }
    sku.value.initSkuData(param)
  })
}

function reback(num: number, type: string = 'update') {
  scene.value = num
  if (type == 'update') {
    getSpuList(currentPage.value)
  } else {
    getSpuList()
  }
}

let skuList = ref<any>([])
let showSkuList = ref(false)
async function lookSkuList(row: any) {
  let result = await reqSkuList(row.id)
  if (result.code === 200) {
    skuList.value = result.data
    showSkuList.value = true
  }
}

async function deleteSpu(id: number) {
  let result = await reqRemoveSpu(id)
  if (result.code === 200) {
    ElMessage.success('删除成功')
    getSpuList(
      recodeds.value.length > 1 ? currentPage.value : currentPage.value - 1,
    )
  } else {
    ElMessage.error('删除失败')
  }
}
//路由组件销毁的时候，把仓库分类相关的数据清空
onBeforeUnmount(() => {
  //清空仓库的数据
  categoryStore.$reset()
})
</script>

<style scoped lang="scss"></style>
